﻿using System.Web.Mvc;
using Limestone;

namespace SampleSite.Areas.Limestone.Controllers
{
    public class SitemapController : Controller
    {
        private readonly IPageProvider _pageProvider;
        private readonly IPageStubCache _pageStubCache;

        public SitemapController(IPageProvider pageProvider, IPageStubCache pageStubCache)
        {
            _pageProvider = pageProvider;
            _pageStubCache = pageStubCache;
        }

        public ActionResult Index()
        {
            return View(_pageProvider.PageStubCache.GetSiteMap(false));
        }

        public ActionResult AddPage(Page page)
        {
            // TODO validate

            // ensure that this is a new page!!! we do not allow editing (yet)
            if (_pageStubCache.GetPageStub(page.Path) != null)
                return Json(null);

            var newPage = _pageProvider.Save(page);
            return Json(newPage);
        }
    }
}